/**
 * Copyright (C) 2009 Ignasi Fosch Alonso
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 2 of the License, or (at your option) any later
 * version.
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 
 * details.
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place, Suite 330, Boston, MA 02111-1307 USA
 *
 * You can reach me by mail at natx@y10k.ws
 * You can reach him by paper mail at:
 * C. LLEIDA, 41-43, 3r 1a
 * El Prat de Llobregat 08820
 * Barcelona, SPAIN
 */
package ws.y10k.goloim.interfaces.swt;

import java.util.Iterator;

import org.eclipse.jface.window.Window;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;

import ws.y10k.goloim.model.Box;

/**
 * @author natx
 *
 */
public class ManageBoxesWindow extends Window {
	
	Table tblBoxes;
	private Label lblWidth;
	Text txtWidth;
	private Label lblHeight;
	Text txtHeight;
	private Label lblDepth;
	Text txtDepth;
	private Label lblWeight;
	Text txtWeight;
	TableItem itmSelected = null;
	private Button btnSave;
	private Button btnCancel;

	public ManageBoxesWindow(Window parent) {
		super(parent);
		this.setShellStyle(SWT.APPLICATION_MODAL | SWT.DIALOG_TRIM);
		this.open();
	}
	
	public Control createContents(Composite parent) {
		// TODO i18n

		parent.getShell().setSize(400, 230);
		parent.getShell().setText("Manage boxes");
		GoLoIm.center(parent.getShell());
		GridLayout lytGrid = new GridLayout();
		lytGrid.numColumns = 2;
		parent.getShell().setLayout(lytGrid);
		
		tblBoxes = new Table(parent.getShell(), SWT.SINGLE | SWT.V_SCROLL);
		GridData grdData = new GridData();
		grdData.heightHint = parent.getShell().getBounds().height - 55;
		tblBoxes.setLayoutData(grdData);
		tblBoxes.setLinesVisible(true);
		tblBoxes.setHeaderVisible(true);
		String[] titles = {"Width","Height","Depth","Weight"};
		for ( int i = 0; i < titles.length; i++ ) {
			TableColumn col = new TableColumn(tblBoxes,SWT.NONE);
			col.setText(titles[i]);
		}
		Iterator<Box> itBoxes = GoLoIm.boxes.iterator();
		while ( itBoxes.hasNext() ) {
			Box box = itBoxes.next();
			TableItem itmBox = new TableItem(tblBoxes,SWT.NONE);
			itmBox.setText(0,((Double) box.getWidth()).toString());
			itmBox.setText(1,((Double) box.getHeight()).toString());
			itmBox.setText(2,((Double) box.getDepth()).toString());
			itmBox.setText(3,((Double) box.getWeight()).toString());
		}
		for ( int i = 0; i < titles.length; i++ ) {
			tblBoxes.getColumn(i).pack();
		}
		tblBoxes.addListener (SWT.Selection, new Listener () {
			@Override
			public void handleEvent (Event e) {
				TableItem [] selection = tblBoxes.getSelection ();
				if ( itmSelected == null || !itmSelected.equals(selection[0]) ) {
					txtWidth.setText(selection[0].getText(0));
					txtHeight.setText(selection[0].getText(1));
					txtDepth.setText(selection[0].getText(2));
					txtWeight.setText(selection[0].getText(3));
					itmSelected = selection[0];
				} else {
					itmSelected = null;
					tblBoxes.deselectAll();
					txtWidth.setText("");
					txtHeight.setText("");
					txtDepth.setText("");
					txtWeight.setText("");
				}
			}
		});
		
		Composite cmpForm = new Composite(parent.getShell(), SWT.BORDER);
		grdData = new GridData();
		grdData.heightHint = parent.getShell().getBounds().height - 50;
		cmpForm.setLayoutData(grdData);
		cmpForm.setSize(tblBoxes.getBounds().height,parent.getShell().getBounds().width - tblBoxes.getBounds().width);
		cmpForm.setLocation(tblBoxes.getBounds().width,0);
		GridLayout lytGridForm = new GridLayout();
		lytGridForm.numColumns = 2;
		cmpForm.setLayout(lytGridForm);
		
		lblWidth = new Label(cmpForm, SWT.NONE);
		lblWidth.setText("&Width");
		grdData = new GridData();
		grdData.widthHint = 50;
		lblWidth.setLayoutData(grdData);
		txtWidth = new Text(cmpForm, SWT.BORDER);
		grdData = new GridData();
		grdData.widthHint = 85;
		txtWidth.setLayoutData(grdData);
		lblHeight = new Label(cmpForm, SWT.NONE);
		lblHeight.setText("&Height");
		grdData = new GridData();
		grdData.widthHint = 50;
		lblHeight.setLayoutData(grdData);
		txtHeight = new Text(cmpForm, SWT.BORDER);
		grdData = new GridData();
		grdData.widthHint = 85;
		txtHeight.setLayoutData(grdData);
		lblDepth = new Label(cmpForm, SWT.NONE);
		lblDepth.setText("&Depth");
		grdData = new GridData();
		grdData.widthHint = 50;
		lblDepth.setLayoutData(grdData);
		txtDepth = new Text(cmpForm, SWT.BORDER);
		grdData = new GridData();
		grdData.widthHint = 85;
		txtDepth.setLayoutData(grdData);
		lblWeight = new Label(cmpForm, SWT.NONE);
		lblWeight.setText("W&eight");
		grdData = new GridData();
		grdData.widthHint = 50;
		lblWeight.setLayoutData(grdData);
		txtWeight = new Text(cmpForm, SWT.BORDER);
		grdData = new GridData();
		grdData.widthHint = 85;
		txtWeight.setLayoutData(grdData);
		btnSave = new Button(cmpForm, SWT.PUSH);
		grdData = new GridData();
		grdData.widthHint = 65;
		grdData.horizontalAlignment = SWT.CENTER;
		btnSave.setLayoutData(grdData);
		btnSave.setText("&Save");
		btnSave.addSelectionListener(new SaveBoxSelectionListener(this));
		btnCancel = new Button(cmpForm, SWT.PUSH);
		grdData = new GridData();
		grdData.widthHint = 65;
		grdData.horizontalAlignment = SWT.CENTER;
		btnCancel.setLayoutData(grdData);
		btnCancel.setText("&Cancel");
		btnCancel.addSelectionListener(new CancelSelectionListener(parent.getShell()));
		txtWidth.setFocus();
		parent.getShell().setDefaultButton(btnCancel);
		
		return parent;
	}
}
